#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class ProfessProvider : CY.GFive.Core.Providers.DALProvider.IProfessProvider
	{
		#region Sql
		private static readonly string SqlSelectProfess = "SELECT [Id], [ProCode],[ProName],[ProShortName],[ProSetting],[Des],[SchoolCate] FROM [Profess] WHERE  [Id]=@Id";

		private static readonly string SqlInsertProfess = "INSERT INTO [Profess]([ProCode],[ProName],[ProShortName],[ProSetting],[Des],[SchoolCate]) values(@ProCode,@ProName,@ProShortName,@ProSetting,@Des,@SchoolCate);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateProfess = "UPDATE [Profess] SET [ProCode] = @ProCode,[ProName] = @ProName,[ProShortName] = @ProShortName,[ProSetting] = @ProSetting,[Des] = @Des,[SchoolCate] = @SchoolCate WHERE  [Id]=@Id";

		private static readonly string SqlDeleteProfess = "DELETE [Profess] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllProfess = "SELECT [Id], [ProCode],[ProName],[ProShortName],[ProSetting],[Des],[SchoolCate] FROM [Profess]";
		#endregion

		#region IProfessProvider Members
		public Core.Business.Profess Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectProfess);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.Profess profess = new Core.Business.Profess();

				if (!reader.IsDBNull(0)) profess.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) profess.ProCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) profess.ProName = reader.GetString(2);
				if (!reader.IsDBNull(3)) profess.ProShortName = reader.GetString(3);
				if (!reader.IsDBNull(4)) profess.ProSetting = reader.GetInt32(4);
				if (!reader.IsDBNull(5)) profess.Des = reader.GetString(5);
				if (!reader.IsDBNull(6)) profess.SchoolCate = reader.GetString(6);

				return profess;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.Profess profess)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ProCode", SqlDbType.NVarChar, profess.ProCode);
			sql.AddParameter("@ProName", SqlDbType.NVarChar, profess.ProName);
			sql.AddParameter("@ProShortName", SqlDbType.NVarChar, profess.ProShortName);
			sql.AddParameter("@ProSetting", SqlDbType.Int, profess.ProSetting);
			sql.AddParameter("@Des", SqlDbType.NVarChar, profess.Des);
			sql.AddParameter("@SchoolCate", SqlDbType.NVarChar, profess.SchoolCate);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertProfess);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) profess.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.Profess profess)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ProCode", SqlDbType.NVarChar, profess.ProCode);
			sql.AddParameter("@ProName", SqlDbType.NVarChar, profess.ProName);
			sql.AddParameter("@ProShortName", SqlDbType.NVarChar, profess.ProShortName);
			sql.AddParameter("@ProSetting", SqlDbType.Int, profess.ProSetting);
			sql.AddParameter("@Des", SqlDbType.NVarChar, profess.Des);
			sql.AddParameter("@SchoolCate", SqlDbType.NVarChar, profess.SchoolCate);
			sql.AddParameter("@Id", SqlDbType.Int, profess.Id);
			sql.ExecuteSql(SqlUpdateProfess);
		}

		public void Delete(Core.Business.Profess profess)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, profess.Id);
			sql.ExecuteSql(SqlDeleteProfess);
		}

		public IList<Core.Business.Profess> GetAllProfess()
		{
			IList<Core.Business.Profess> professlist = new List<Core.Business.Profess>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllProfess);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.Profess profess = new Core.Business.Profess();

					if (!reader.IsDBNull(0)) profess.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) profess.ProCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) profess.ProName = reader.GetString(2);
					if (!reader.IsDBNull(3)) profess.ProShortName = reader.GetString(3);
					if (!reader.IsDBNull(4)) profess.ProSetting = reader.GetInt32(4);
					if (!reader.IsDBNull(5)) profess.Des = reader.GetString(5);
					if (!reader.IsDBNull(6)) profess.SchoolCate = reader.GetString(6);

					profess.MarkOld();
					professlist.Add(profess);
				}
				reader.Close();
			}
			return professlist;
		}
		#endregion
	}
}
